﻿using EFCore3.Entity;
using EFCore3.Entity.树形结构;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Net.Http.Headers;

namespace EFCore3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using(var db = new sqlContext())
            {
                // 添加
                //var artical = new Article() { Title = "color", Content = "this is a color" };
                //artical.Commments.Add(new Comment() { Message=" purple very nice" });
                //db.Articles.Add(artical);
                // db.SaveChanges();

                // 查询
                //var theArticle = db.Articles.Include(p=>p.Commments).FirstOrDefault(p=>p.Title == "title");
                //Console.WriteLine(theArticle.Title + "-" + theArticle.Content);

                //var theCommments = db.Comments.Include(p => p.LinkArticle).FirstOrDefault(p=>p.Message.Contains("purple"));
                //Student st = new Student() { Data = "true" };
                //var field = st.GetType().GetProperties();
                //foreach(var item in field)
                //{
                //    var attribute = (DescriptionAttribute?)Attribute.GetCustomAttribute(item, typeof(DescriptionAttribute));
                //    var attribute1 = (DisplayAttribute?)Attribute.GetCustomAttribute(item, typeof(DisplayAttribute));
                //    var attr = item.GetCustomAttributes(false);//.FirstOrDefault();
                //    Console.WriteLine(item.Name+":"+ item.GetValue(st)+":"+ attribute.Description);
                //}

                ///  一对一 
               //// var order = new Order() { Name = "Orange" };
               // var delivery = new Delivery() { CompanyName = "SF"};
               //// db.Add<Order>(order);
               // db.Add<Delivery>(delivery);
               // db.SaveChanges();
               // var list = db.Orders.Include(p => p.Delivery).Where(p => p.Name == "Orange");
               // foreach (var item in list)
               // {
               //     Console.WriteLine(item.Name + "-" + item.Delivery?.CompanyName);
               // }

                // 删除
                //var order = db.Orders.Where(o => o.Name == "Orange").FirstOrDefault();
                //db.Remove<Order>(order);
                //db.SaveChanges();

                // 一对多关系
                //var st1 = new Student() { Name = "张三" };
                //var st2 = new Student() { Name = "李四" };
                //var st3 = new Student() { Name = "王五" };
                //var t1 = new Teacher() { Name = "张老师"};
                //var t2 = new Teacher() { Name = "王老师" };
                //var t3 = new Teacher() { Name = "关颖" };
                //t1.Students.Add(st1);
                //t1.Students.Add(st2);
                //t2.Students.Add(st2);
                //t2.Students.Add(st3);
                //t3.Students.Add(st1);

                //db.Students.Add(st1);
                //db.Students.Add(st2);
                //db.Students.Add(st3);
                //db.Teachers.Add(t1);
                //db.Teachers.Add(t2);
                //db.Teachers.Add(t3);
                //db.SaveChanges();

                // 树形结构
                var parent = new Menu() { Name = "根节点" };
                var child1 = new Menu() { Name = "一级菜单1", Parent = parent };
                var child2 = new Menu() { Name = "一级菜单2", Parent = parent };
                var child3 = new Menu() { Name = "一级菜单3", Parent = parent };
                var child11 = new Menu() { Name = "二级菜单1-1", Parent = child1 };
                db.Menus.Add(parent);
                db.Menus.Add(child1);
                db.Menus.Add(child2);
                db.Menus.Add(child3);
                db.Menus.Add(child11);
                db.SaveChanges();
                Console.ReadKey();
            }
        }
    }
}